package com.example.emptytest;

import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.emptytest.DB.SQLiteHelper;
import com.example.emptytest.R;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public class Fragment_0_0 extends Fragment implements View.OnClickListener {
    private Button mBtn_save;
    private String STNum;
    private SQLiteHelper helper;
    private Cursor cursor;
    TextView EditName,EditNum,EditPhoneNum;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_0_0,container,false);
        STNum = getArguments().getString("STNum");
        helper = new SQLiteHelper(getActivity());
        initView(view);
        return view;
    }

    private void initView(View view) {
        mBtn_save = view.findViewById(R.id.btm_save);
        mBtn_save.setOnClickListener(this);
        EditName = view.findViewById(R.id.editName);
        EditNum = view.findViewById(R.id.editNum);
        EditPhoneNum = view.findViewById(R.id.phoneNum);

        String args[] = new String[]{"%" + STNum + "%"};
        cursor = helper.query(args);
        if(cursor.moveToFirst()) {//cursor读取数据
            EditName.setText(cursor.getString(cursor.getColumnIndex("STName")));
            EditNum.setText(cursor.getString(cursor.getColumnIndex("STNum")));
            EditPhoneNum.setText(cursor.getString(cursor.getColumnIndex("PhoneNum")));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btm_save:
                System.out.println("存储个人信息");
                String Name;
                String Num;
                String Pswd;
                String Phone;
                int ID;
                if(cursor.moveToFirst()) {//cursor读取数据
                    Name = cursor.getString(cursor.getColumnIndex("STName"));
                    Num = STNum;
                    Pswd = cursor.getString(cursor.getColumnIndex("STPswd"));
                    Phone = EditPhoneNum.getText().toString();
                    ID = cursor.getInt(cursor.getColumnIndex("_id"));
                    helper.update(ID,Num,Pswd,"1",Name,Phone);
                    Toast toast1 = Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT);
                    toast1.setGravity(Gravity.CENTER, 0, 0);
                    toast1.show();
                }
                break;
        }
    }
}
